<?php
$this->Html->scriptBlock($script, array('inline' => false));
$this->Html->scriptBlock($getJSON, array('inline' => false));
?>
<div class="title-body-content"><?php echo $title_for_layout; ?></div>
<?php
$formUrl = array('controller' => 'aihexpenses', 'action' => 'add');
echo $this->Form->create('Aihexpense', array('url' => $formUrl, 'type' => 'text'));
?>
<div class="wrapper-content-body clearfix">
    <div class="wrapper-form clearfix">
        <div class="wrapper-aih-expense-ref ">
            <span class="size-span"><?php echo __('Acc resume related'); ?> :</span>
            <input type="checkbox" id="AccResumeRelated" name="AccResumeRelated" value="1" checked />
        </div>
        <div class="wrapper-aih-expense-ref " id="accresume">
            <span class="size-span"><?php echo __('Reference#'); ?> :</span>
            <?php
            echo $this->Form->input('Accountresume.Accountresume', array(
                'multiple' => false,
                'empty' => false,
                'class' => 'sel-ref-aih-expense',
                'label' => false,
                'div' => false,)
            );
            ?>
        </div>
        <div class="wrapper-aih-expense-ref " id="paymentdetail">
            <span class="size-span"><?php echo __('Payment Schedule #'); ?> :</span>
            <?php
            echo $this->Form->input('Paymentschedule.Paymentschedule', array(
                'multiple' => false,
                'empty' => false,
                'class' => 'sel-ref-aih-expense',
                'label' => false,
                'div' => false,)
            );
            ?>
        </div>
        <div class="line-form-aih">
            <span class="size-span"><?php echo __('Date'); ?> (*):</span> 
            <?php
            echo $this->Form->text('date', array(
                'class' => 'sel-date',
                'readonly' => 'readonly',
                'label' => false,
                'div' => false,)
            );
            ?>
        </div>
        <div class="line-form-aih clearfix">
            <span class="size-span"><?php echo __('Reference of Transaction'); ?> (*):</span>
            <?php
            echo $this->Form->text('reference_of_transaction', array(
                'class' => 'aih-expense-amount-number',
                'style' => 'width: 300px;',
                'label' => false,
                'div' => false,)
            );
            ?>
            <span style="font-style: italic;">Your Bank supplied you after transaction completed.</span>
        </div>
        <div class="clearfix">
            <span class="size-span"></span>
            <span id="exsisterror"></span>
        </div>
        <div class="line-form-aih clearfix">
            <span class="size-span"><?php echo __('From'); ?> (*):</span>
            <?php
            echo $this->Form->textarea('from', array(
                'class' => 'aih-expense-textarea-from',
                'label' => false,
                'div' => false,)
            );
//            explode(PHP_EOL,$string);
            ?>
        </div>

        <div class="line-form-aih clearfix">
            <span class="size-span"><?php echo __('To'); ?> (*):</span>
            <?php
            $value = "NAME of BANK:\nBank Code:\nBranch Code:\nADDRESS of BANK:\nSWIFT:";
            echo $this->Form->textarea('to', array(
                'value' => $value,
                'class' => 'aih-expense-textarea-from',
                'label' => false,
                'div' => false,)
            );
            ?>
        </div>

        <div class="line-form-aih">
            <span class="size-span"><?php echo __('Amount'); ?> (*):</span>
            <?php
            echo $this->Form->text('amount', array(
                'class' => 'aih-expense-amount-number',
                'label' => false,
                'div' => false,)
            );
            ?>
            <?php
            echo $this->Form->input('Currency.Currency', array(
                'multiple' => false,
                'empty' => false,
                'class' => '',
                'label' => false,
                'div' => false,)
            );
            ?>
            <?php
            echo $this->Form->text('amount_note', array(
                'class' => 'aih-expense-amount-char',
                'label' => false,
                'div' => false,)
            );
            ?>
        </div>
        <div class="line-form-aih clearfix">
            <span class="size-span"><?php echo __('Description'); ?> :</span>
            <?php
            echo $this->Form->textarea('description', array(
                'class' => 'aih-expense-textarea-description',
                'label' => false,
                'div' => false,)
            );
            ?>
        </div>
        <div class="line-form-aih">
            <span class="size-span"><?php echo __('Charge Payer'); ?> :</span>
            <?php
            echo $this->Form->input('Aihcontact.Aihcontact', array(
                'multiple' => false,
                'empty' => false,
                'class' => 'sel-ref-aih-expense',
                'label' => false,
                'div' => false,)
            );
            ?>
        </div>
    </div>
</div>

<!-- HTML for buttom -form-->

<div class="wrapper-button-form">
    <div class="button-form">
        <div class="button-aihre button-blue"><a href="#" class="save">SAVE</a></div>
        <div class="button-aihre button-blue"><?php echo $this->Html->link(__('CANCEL'), array('plugin' => null, 'controller' => 'aihexpenses', 'action' => 'index')); ?></div>
    </div>
</div>
<?php
echo $this->Form->end();
?>
<?php
$validForm = "
    $(function(){
        $('#AccResumeRelated').click(function(){
            if($(this).is(':checked')){
                $('#paymentdetail').attr('style','display:block');
                $('#accresume').attr('style','display:block');
            }else{
                $('#paymentdetail').attr('style','display:none');
                $('#accresume').attr('style','display:none');
            }
        });
        $('#AihexpenseAmount').change(function(){
            $(this).formatCurrency();
        });
        
        $('a.save').click(function(){
            if($('#PaymentschedulePaymentschedule').val() == null && $('#AccResumeRelated').is(':checked') ){
                alert('Can not create expense for this referrence because it is is full of expense.');
            }else if($('#AihexpenseDate').val() == ''){
                alert('Date field not allow null.');
                $('#AihexpenseDate').focus();
            }else if($('#AihexpenseFrom').val() == ''){
                alert('From field not allow null.');
                $('#AihexpenseFrom').focus();
            }else if($('#AihexpenseAmount').val() == ''){
                alert('Amount field not allow null.');
                $('#AihexpenseAmount').focus();
            }else if($('#AihexpenseReferenceOfTransaction').val() == ''){
                alert('Reference of Transaction field not allow null.');
                $('#AihexpenseReferenceOfTransaction').focus();
            }else{
                $.getJSON('" . Router::url('/admin/aihexpenses/checkreftransunique/', true) . "'+$('#AihexpenseReferenceOfTransaction').val(), function(data) {
                    if(data.success){
                        if(data.isExsist === '1'){
                            $('#exsisterror').html('This Reference of Transactions has existed.');
                            $('#exsisterror').attr('style','color:red');
                            $('#AihexpenseReferenceOfTransaction').focus();
                        }else{
                            $('#exsisterror').removeAttr('style');
                            $('#exsisterror').html('');
                            $('#AihexpenseAdminAddForm').submit();
                        }
                    } else {
                        // error
                    }
                });
            }
        });
        
        $('#AihexpenseAmount').blur(function(){
            $('#AihexpenseAmount').formatCurrency();
        });
    });";
$this->Html->scriptBlock($validForm, array('inline' => false));
?>